/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package knockknock.app;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

import knockknock.In;
import knockknock.Out;

/**
 * Client part of the socket-based KnockKnock communication
 */
public class KnockKnockClientApp {

    public static void main(String[] args) throws IOException {

        final String host = "localhost";
        Socket kkSocket = null;
        DataOutputStream out = null;
        DataInputStream in = null;

        try {
            kkSocket = new Socket(host, 4444);
            out = new DataOutputStream(kkSocket.getOutputStream());
            in = new DataInputStream(kkSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + host + ".");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: " + host + ".");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        Out fromServer;
        In fromUser;
        
        while (kkSocket.isBound()) {
			fromServer = new Out();
			fromServer.read(in);
			System.out.println("Server: " + fromServer);
			if (fromServer.isEqual("Bye.")) {
				kkSocket.close();
				break;
			}
			
			String userInput = stdIn.readLine();
			if(userInput != null) {
				try {
					fromUser = new In();
					fromUser.setVchars(userInput);
					System.out.println("Client: " + fromUser);
					fromUser.write(out);
					out.flush();
				} catch(IOException e) {
					System.err.println("Could not send message '" + userInput + "' to server.");
					e.printStackTrace();
				}
			}
		}

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}
